package com.jfinal.aceadmin.model;

import java.util.Date;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Repository;

import com.jfinal.aceadmin.util.DateUtil;
import com.jfinal.aceadmin.util.DesUtil;
import com.jfinal.aceadmin.util.ValidateUtil;
import com.jfinal.aceadmin.vo.InformVo;
import com.jfinal.aceadmin.vo.MessageVo;
import com.jfinal.aceadmin.vo.NotificationVo;
import com.jfinal.aceadmin.vo.TaskVo;

@Repository
public class UserImpl {

	private static ApplicationContext ctx;

	public static UserImpl getDAO() {
		if (null == ctx) {
			ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		}
		UserImpl instance = ctx.getBean(UserImpl.class);
		return instance;
	}

	/**
	 * User login logic
	 * 
	 * @param emailAddress
	 * @param password
	 * @return
	 */
	public User login(String emailAddress, String password) {
		User user = User.dao.findFirst(
				"SELECT * FROM User WHERE emailAddress = ? OR name = ?",
				emailAddress, emailAddress);
		if (null != user) {
			String pass = user.getStr("password");
			String email = user.getStr("emailAddress");
			String encryptPass = DesUtil.decrypt(pass, email);
			if (encryptPass.equals(password.trim())) {
				user.set("lastLoginDate", new Date()).update();
				return user;
			}
		}
		return null;
	}

	/**
	 * User infrom info
	 * 
	 * @param name
	 * @return
	 */
	public InformVo getUserInformInfo(String name) {
		InformVo informVo = new InformVo();
		User user = User.dao.findByName(name);
		if (null != user) {
			int userId = user.getId();
			int tasks = UserTask.dao.countByUserId(userId);
			int notifications = UserNotification.dao.countByUserIdAndRead(userId, false);
			int messages = UserMessage.dao.countByUserIdAndRead(userId, false);
			informVo.setMessages(messages);
			informVo.setNotifications(notifications);
			informVo.setTasks(tasks);

			List<TaskVo> taskVos = UserTaskImpl.getDAO().getTasks(userId);
			List<NotificationVo> notificationVos = UserNotificationImpl
					.getDAO().getNotifications(userId);
			List<MessageVo> messageVos = UserMessageImpl.getDAO().getMessages(
					userId);

			informVo.setMessageVos(messageVos);
			informVo.setTaskVos(taskVos);
			informVo.setNotificationVos(notificationVos);

		}
		return informVo;
	}

	/**
	 * check email exist
	 * 
	 * @param emailAddress
	 * @return
	 */
	public boolean existEmail(String emailAddress) {
		User user = User.dao.findByEmail(emailAddress);
		if (null == user) {
			return true;
		}
		return false;
	}

	/**
	 * check name exist
	 * 
	 * @param name
	 * @return
	 */
	public boolean existName(String name) {
		User user = User.dao.findByName(name);
		if (null == user) {
			return true;
		}
		return false;
	}

	/**
	 * validate the user info
	 * 
	 * @param emailAddress
	 * @param name
	 * @param password
	 * @param birthday
	 * @return
	 */
	public boolean validateUser(String emailAddress, String name,
			String password, String birthday) {
		Date birthDate = DateUtil.parseDate(birthday);
		int age = DateUtil.getYearsBeforeNow(birthDate);

		boolean result = ValidateUtil.isEmailValid(emailAddress)
				&& ValidateUtil.isNameValid(name)
				&& ValidateUtil.isPasswordValid(password)
				&& ValidateUtil.isAgeValid(age + "");

		return result && existEmail(emailAddress) && existName(name);
	}
}
